package com.api.workflow.action;

import com.api.workflow.util.CrmUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

//修改销售订单
public class ModifySalesOrderAction implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        try  {
            // 表单名称
            String tableName = requestInfo.getRequestManager().getBillTableName();
            // 表单数据ID
            int bill_id = requestInfo.getRequestManager().getBillid();

            String sql = "select * from "+ tableName +" where id = "+ bill_id;
            new BaseBean().writeLog("ModifySalesOrderAction ------------------------> sql:"+ sql);
            RecordSet recordSet = new RecordSet();
            recordSet.execute(sql);
            // crm 对应 oa字段
            HashMap<String, String> mainTableInfoMap = new HashMap<String, String>();
            mainTableInfoMap.put("oa_status__c", "zt");  // 审批状态
//        mainTableInfoMap.put("approval_node__c", "nodename");  // 审批节点
//        mainTableInfoMap.put("approval_opinion__c", "");  // 审批意见
            if (recordSet.next()){
                HashMap<String, String> param = new HashMap<String, String>();
                for (Map.Entry<String, String> stringStringEntry : mainTableInfoMap.entrySet()) {
                    param.put(stringStringEntry.getKey(), recordSet.getString(stringStringEntry.getValue()));
                }
                String token = CrmUtil.getToken();
                param.put("_id", recordSet.getString("crmid"));
                String remark = requestInfo.getRequestManager().getRemark(); //流程意见
                param.put("approval_opinion__c", remark);
                String requestid = requestInfo.getRequestid(); // 请求ID
                RecordSet re = new RecordSet();
                re.executeQuery(" select nodename from workflow_nodebase where id = (select currentnodeid from workflow_requestbase where requestid = ?)",requestid);
                if (re.next()){
                    param.put("approval_node__c", re.getString("nodename"));
                }
                boolean b = CrmUtil.updateSaleOrder(token,param);

                if (b){
                    new BaseBean().writeLog("ModifySalesOrderAction ------------------------> 修改销售订单状态成功");
                }else {
                    new BaseBean().writeLog("ModifySalesOrderAction ------------------------> 修改销售订单状态失败");
                    requestInfo.getRequestManager().setMessageid("11111"+requestInfo.getRequestid()+"22222");
                    requestInfo.getRequestManager().setMessagecontent("修改销售订单状态失败("+ requestid +"):"+param.get("errorMsg"));
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
        } catch (Exception e) {
            new BaseBean().writeLog("ModifySalesOrderAction ------------------------> e:"+  e);
        }
        return null;
    }
}
